import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../pages/Home.vue')
  },
  {
    path: '/events',
    name: 'Events',
    component: () => import('../pages/Events.vue')
  },
  {
    path: '/events/:id',
    name: 'EventDetail',
    component: () => import('../pages/EventDetail.vue')
  },
  {
    path: '/venues',
    name: 'Venues',
    component: () => import('../pages/Venues.vue')
  },
  {
    path: '/venues/:id',
    name: 'VenueDetail',
    component: () => import('../pages/VenueDetail.vue')
  },
  {
    path: '/orders',
    name: 'Orders',
    component: () => import('../pages/Orders.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../pages/Profile.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../pages/Login.vue')
  },
{
  path: '/register',
  name: 'Register',
  component: () => import('../pages/Register.vue')
},
{
  path: '/test-api',
  name: 'TestApi',
  component: () => import('../pages/TestApi.vue')
},
  // 管理员路由
  {
    path: '/admin/events',
    name: 'AdminEvents',
    component: () => import('../pages/admin/AdminEvents.vue'),
    meta: {
      requiresAuth: true
    }
  },
  {    path: '/admin/venues',
    name: 'AdminVenues',
    component: () => import('../pages/admin/AdminVenues.vue'),
    meta: {
      requiresAuth: true
    }
  },
  {    path: '/admin/users',
    name: 'AdminUsers',
    component: () => import('../pages/admin/AdminUsers.vue'),
    meta: {
      requiresAuth: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router